package com.util.mapdemo;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.OptionalInt;
import java.util.UUID;

/**
 * @Author:zxh
 * @Description: UUID，Base64 - test
 * @Date: 2020/10/31
 * @Modified By:
 */
public class UuidAndBase64 {

    /**
     * 一个表示不可变的通用唯一标识符（UUID）的类。
     */
    public void uuidTest() {
        String replaceAll = UUID.randomUUID().toString().replaceAll("-", "");
        System.out.println("get UUID:" + replaceAll);
    }

    /**
     * Base64
     *
     * @param
     */
    public void baseTest() throws UnsupportedEncodingException {
        //编码
        String encodeToString = Base64.getEncoder().encodeToString("runoob?java8".getBytes("utf-8"));
        System.out.println("Base64 编码字符串(基本)：" + encodeToString);

        //解码
        byte[] decode = Base64.getDecoder().decode(encodeToString);
        System.out.println("原始字符串： " + new String(decode, "utf-8"));

        encodeToString = Base64.getUrlEncoder().encodeToString("TutorialsPoint?java8".getBytes("utf-8"));
        System.out.println("Base64 编码字符串-URL：" + encodeToString);

        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 10; i++) {
            stringBuilder.append(UUID.randomUUID().toString());
        }
        byte[] bytes = stringBuilder.toString().getBytes("utf-8");
        String string = Base64.getMimeEncoder().encodeToString(bytes);
        System.out.println("base 64 编码字符串-MIME：" + string);
    }

    /**
     * 这3个类并不支持ofNullable()，filter()，map()，flatMap()
     *
     * @param
     * @throws
     */
    public void optionalTest() {
        //静态工厂方法获取一个示例
        OptionalInt optionalInt = OptionalInt.of(1);
        if (optionalInt.isPresent()) {
            //获得OptionalInt对象里面的值，输出1
            System.out.println(optionalInt.getAsInt());
        }

        optionalInt.ifPresent(value -> System.out.println("value: " + optionalInt.getAsInt()));

        //创建一个空值对象 ==========
        OptionalInt empty = OptionalInt.empty();
        if (empty.isPresent()) {
            //和Optional一样，输出No value present
            System.out.println(empty.getAsInt());
        } else {
            //如果没有值，赋初始值  这里，现在这里只能写int值
            System.out.println(empty.orElse(222));
            //如果没有值，赋初始函数
            System.out.println(empty.orElseGet(() -> 333));
        }
        //如果【没有值】则抛出异常
        empty.orElseThrow(NullPointerException::new);
    }


    public static void main(String[] args) throws UnsupportedEncodingException {
        //new UuidAndBase64().uuidTest();
        //new UuidAndBase64().baseTest();
        new UuidAndBase64().optionalTest();
    }
}
